/**
 * Created with  SublimeText
 * @Author:      Bert
 * @DateTime:    2017-1-9 10:19:40
 * @Description: 
 */

define(function(require, exports, module) {

    var tpl = require('{base}/tpls/myProfileComments.tpl'),
        tplItem = require('{base}/tpls/myProfileCommentsItem.tpl');
    var maxPageNum = 20,
        condition = {};

    var component = {

        template: _.template(tpl),
        templateI: _.template(tplItem),

        render: function(page, obj) {
            this.page = page;
            return this.template(this.getTplData());
        },

        componentMount: function() {
            var userInfo = JSON.parse(Util.getSessionStorage('userInfo'));
            condition = {
                page_num: 0,
                from: 0,
                offset: 20,
                prof_id: userInfo.account.usr_id
            };
            this.bindEvt();
            this.myProfileCommentsRender();
        },

        componentUpdate: function() {

        },

        bindEvt: function() {
            var tthis = this,
                Dom = $('#' + this.page.id).find('.tabBox').find('div[tabname=\'myProfileComments\']');

            Dom.find('.commentsList').on('click', '.hide', function(e) {
                var comment_id = $(e.target).closest('.commentsListItem').attr('cid');
                tthis.ajaxStatus({ comment_id: comment_id, type: 'hide' });
            });

            Dom.find('.commentsList').on('click', '.restore', function(e) {
                var comment_id = $(e.target).closest('.commentsListItem').attr('cid');
                tthis.ajaxStatus({ comment_id: comment_id, type: 'show' });
            });

            Dom.find('.commentsList').on('click', '.more', function(e) {
                console.log('ajax');
            });

        },

        getTplData: function() {
            var obj = {
                userInfo: JSON.parse(Util.getSessionStorage('userInfo')),
                userStatus: JSON.parse(Util.getSessionStorage('userStatus'))
            }
            return obj;
        },

        myProfileCommentsRender: function() {
            this.getCommentsAjax(condition);
        },

        affectOtherChange: function(typeString) {
            if (this.page.myProfileCommentsComponentChange) {
                this.page.myProfileCommentsComponentChange(typeString);
            }
        },

        getCommentsAjax: function(data) {
            var tthis = this,
                Dom = $('#' + this.page.id).find('.tabBox').find('div[tabname=\'myProfileComments\']'),
                contentDom = Dom.find('.commentsList'),
                ajaxObj = {
                    url: seajs.data.vars.apiUrl + 'get_profile_comments',
                    type: 'POST',
                    data: data,
                    success: function(data) {
                        tthis.renderComments(data);
                    },
                    complete: function(xhr, status) {
                        Share.ajaxEndB(contentDom);
                    }
                };
            Share.ajaxStartB(contentDom, true);
            $.ajax(Share.ajaxControl(ajaxObj));
        },

        renderComments: function(data) {
            var html = '',
                Dom = $('#' + this.page.id).find('.tabBox').find('div[tabname=\'myProfileComments\']'),
                Box = Dom.find('.commentsList').find('dd');

            for (var i in data) {
                html += this.createComment(data[i]);
            }

            if (data.length == 0 && $.trim(Box.html()) == '') {
                html = 'There are no new comments / testimonials about you.';
                Dom.find('.more').hide();
            } else if (data.length < maxPageNum) {
                Dom.find('.more').hide();
            } else {
                Dom.find('.more').show();
            }

            Box.append(html);
            Box.imglazyload({ sH: 60, sW: 60 });
        },

        createComment: function(obj) {
            return this.templateI(obj);
        },

        ajaxStatus: function(data) {
            var tthis = this,
                ajaxObj = {
                    url: seajs.data.vars.apiUrl + data.type + '_profile_comment',
                    type: 'POST',
                    data: data,
                    success: function(callback) {
                        tthis.toggleStatus(data);
                    }
                };
            $.ajax(Share.ajaxControl(ajaxObj));
        },

        toggleStatus: function(data) {
            let Dom = $('#' + this.page.id).find('.tabBox').find('div[tabname=\'myProfileComments\']'),
                contentDom = Dom.find('.commentsList').find('.commentsListItem[cid=="' + data.comment_id + '"]');
            if (type == 'hide') {
                contentDom.find('.commentsListOpe').hide();
                contentDom.find('.commentsListRestore').show();
            } else {
                contentDom.find('.commentsListOpe').show();
                contentDom.find('.commentsListRestore').hide();
            }
        },


    }

    module.exports = component;
});
